import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.*;

public class Popup implements ActionListener{
  JPopupMenu Pmenu;
  JMenuItem menuItem;
  JTextField txt;
  int ctr=0;
  public static void main(String[] args) {
  Popup p = new Popup();
  }

  public Popup(){
  JFrame frame = new JFrame("Creating a Popup Menu");
  txt=new JTextField(15);
  
  Pmenu = new JPopupMenu();
  menuItem = new JMenuItem("Nick");
  menuItem.addActionListener(this);
  Pmenu.add(menuItem);
  menuItem = new JMenuItem("made");
  Pmenu.add(menuItem);
  menuItem.addActionListener(this);
  menuItem = new JMenuItem("This");
  Pmenu.add(menuItem);
  menuItem.addActionListener(this);
  menuItem = new JMenuItem("You can");
  Pmenu.add(menuItem);
  menuItem.addActionListener(this);
  menuItem = new JMenuItem("do it to");
  Pmenu.add(menuItem);
  menuItem.addActionListener(this);
  frame.addMouseListener(new MouseAdapter(){
  public void mouseReleased(MouseEvent Me){
  if(Me.isPopupTrigger()){
  Pmenu.show(Me.getComponent(), Me.getX(), Me.getY());
  }
  }
  });
  frame.setLayout(new FlowLayout());
  frame.add(txt);
  frame.setSize(400,400);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public void actionPerformed(ActionEvent e) { 
		ctr++;
		txt.setText("PopupMenu Clicks: "+ctr); 
          }
}